package com.eboss2.sbs.adapter;

import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.eboss2.sbs.R;
import com.views.apptwolistview.model.FirstClassItem;
import com.views.apptwolistview.model.SecondClassItem;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2018/6/12 0012.
 */

public class MyExpandableListViewAdapter extends BaseExpandableListAdapter {

    private Map<String, List<SecondClassItem>> datasets = new HashMap<>();

    private String[] PList;
    private String[] PListId;

//    private List<FirstClassItem> parentEntities;
//    private List<SecondClassItem> childEntities;

    private Context mContext;

    TextView parentText;


    public MyExpandableListViewAdapter(Map<String, List<SecondClassItem>> datasets, String[] PListId, String[] PList, Context mContext) {
        this.datasets = datasets;
        this.PList = PList;
        this.PListId = PListId;
//        this.parentEntities = parentEntities;
//        this.childEntities = childEntities;
        this.mContext = mContext;
    }

    //  获得某个父项的某个子项
    @Override
    public Object getChild(int parentPos, int childPos) {
        return datasets.get(PListId[parentPos]).get(childPos);
    }

    //  获得父项的数量
    @Override
    public int getGroupCount() {
        if (datasets == null) {
            Toast.makeText(mContext, "dataset为空", Toast.LENGTH_SHORT).show();
            return 0;
        }
        return datasets.size();
    }

    //  获得某个父项的子项数目
    @Override
    public int getChildrenCount(int parentPos) {
        if (datasets.get(PListId[parentPos]) == null) {
//            Toast.makeText(mContext, "没有子项数据为空", Toast.LENGTH_SHORT).show();
            return 0;
        }
        return datasets.get(PListId[parentPos]).size();
    }

    //  获得某个父项
    @Override
    public Object getGroup(int parentPos) {
        return datasets.get(PListId[parentPos]);
    }

    //  获得某个父项的id
    @Override
    public long getGroupId(int parentPos) {
        return parentPos;
    }

    //  获得某个父项的某个子项的id
    @Override
    public long getChildId(int parentPos, int childPos) {
        return childPos;
    }

    //  按函数的名字来理解应该是是否具有稳定的id，这个函数目前一直都是返回false，没有去改动过
    @Override
    public boolean hasStableIds() {
        return false;
    }

    //  获得父项显示的view
    @Override
    public View getGroupView(int parentPos, boolean b, View view, ViewGroup viewGroup) {
        if (view == null) {
            LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            view = inflater.inflate(R.layout.elv_cinema_list_item_cityproper, null);
        }
        view.setTag(R.layout.elv_cinema_list_item_cityproper, parentPos);
        view.setTag(R.layout.elv_cinema_list_item_cinema, -1);
        parentText = (TextView) view.findViewById(R.id.parent_title);
        ImageView parent_img = view.findViewById(R.id.parent_img);
//            设置展开和收缩的文字颜色
        if (b) {
            parentText.setTextColor(Color.parseColor("#333333"));
            parent_img.setImageResource(R.drawable.icon_down_arrow2);
        } else {
            parentText.setTextColor(Color.BLACK);
            parent_img.setImageResource(R.drawable.icon_arrow_r2);
        }
        parentText.setText(PList[parentPos]);
        return view;
    }

    //  获得子项显示的view
    @Override
    public View getChildView(final int parentPos, final int childPos, boolean b, View view, ViewGroup viewGroup) {
        if (view == null) {
            LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            view = inflater.inflate(R.layout.elv_cinema_list_item_cinema, null);
        }
        view.setTag(R.layout.elv_cinema_list_item_cityproper, parentPos);
        view.setTag(R.layout.elv_cinema_list_item_cinema, childPos);
        TextView text = (TextView) view.findViewById(R.id.child_title);
//        TextView textView = view.findViewById(R.id.child_message);
        text.setText(datasets.get(PListId[parentPos]).get(childPos).getName());
//        textView.setText(datasets.get(PList[parentPos]).get(childPos).getCinema_address());
        return view;
    }
    //  子项是否可选中，如果需要设置子项的点击事件，需要返回true
    @Override
    public boolean isChildSelectable(int i, int i1) {
        return true;
    }
}

